package com.voltmao.webpro.utils.wechat.utils;

import com.thoughtworks.xstream.XStream;
import com.voltmao.webpro.utils.http.HttpClientUtil;
import com.voltmao.webpro.utils.wechat.IWechatUtils;
import com.voltmao.webpro.utils.wechat.core.AccessToken;
import com.voltmao.webpro.utils.wechat.core.Account;
import com.voltmao.webpro.utils.wechat.utils.dto.msg.TemplateMsg;
import com.voltmao.webpro.utils.wechat.utils.dto.msg.response.ArticleItemMsg;
import com.voltmao.webpro.utils.wechat.utils.dto.msg.response.SendNews;
import com.voltmao.webpro.utils.wechat.utils.dto.msg.response.SendNewsMsg;
import com.voltmao.webpro.utils.wechat.utils.dto.msg.response.SendTxtMsg;
import net.sf.json.JSONObject;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * 微信 消息工具类
 */
public class MsgUtils {

    public interface SubscribeMsgCallback {

        ArticleItemMsg success();
    }


    private static Logger logger = Logger.getLogger(MsgUtils.class.getName());
    private Map<String, String> wxConfig;
    private AccessToken accessToken;
    private Account account;
    private ArticleItemMsg subscribeMsg;                                                //关注后发送的信息对象


    public MsgUtils(IWechatUtils wechatUtils) {
        this.wxConfig = wechatUtils.getWxConfig();
        this.accessToken = wechatUtils.getAccessToken();
        this.account = wechatUtils.getAccount();
    }

    /**
     * 发送模板消息
     *
     * @param templete
     */
    public void sendTemplateMsg(TemplateMsg templete) {
        //转JSON
        String sendUrl = wxConfig.get("wx.oauth2.template").replace("ACCESS_TOKEN", accessToken.getAccessToken());
        try {
            logger.info("JSON =>" + templete.asJson());
            JSONObject responseObj = HttpClientUtil.doPostStr(sendUrl, templete.asJson());
            logger.info("HTTP responseObj:" + responseObj);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//        public void send

    /**
     * 加载关注后信息
     *
     * @param reqMap
     * @param callback
     * @return
     */
    public String loadSubscribeMsg(Map<String, Object> reqMap, SubscribeMsgCallback callback) {
        //通过回调获取关注消息
        if (subscribeMsg == null) {
            subscribeMsg = callback.success();
        }

        if (subscribeMsg == null) {
            logger.info("发送关注信息失败: subscribeMsg is null");
            return "";
        }

        SendNewsMsg sendNewsMsg = new SendNewsMsg();
        sendNewsMsg.setFromUserName(reqMap.get("ToUserName") + "");
        sendNewsMsg.setToUserName(reqMap.get("FromUserName") + "");
        sendNewsMsg.setCreateTime(reqMap.get("CreateTime") + "");
        sendNewsMsg.setMsgType("news");
        sendNewsMsg.addArticles(subscribeMsg);

        //对象转换为XML字符串
        XStream xstream = new XStream();
        xstream.alias("xml", sendNewsMsg.getClass());
        xstream.alias("item", new SendNews().getClass());

        return xstream.toXML(sendNewsMsg);
    }

    /**
     * 构造图文消息
     *
     * @param openid
     * @param articleItemMsg
     * @return
     */
    public void builderNewsMsg(String openid, List<ArticleItemMsg> articleItemMsg) {

        SendNewsMsg sendNewsMsg = new SendNewsMsg();
        sendNewsMsg.setFromUserName(account.getOriginId());
        sendNewsMsg.setToUserName(openid);
        sendNewsMsg.setCreateTime(new Date().getTime() + "");

        for (ArticleItemMsg item : articleItemMsg) {
            sendNewsMsg.addArticles(item);
        }
        sendNewsMsg.setMsgType("news");

        //对象转换为XML字符串
        XStream xstream = new XStream();
        xstream.alias("xml", sendNewsMsg.getClass());
        xstream.alias("item", new SendNews().getClass());

//            return xstream.toXML(sendNewsMsg);
    }

    /**
     * 构造文本消息
     *
     * @param sendTxtMsg
     * @return
     */
    public String builderTxtMsg(SendTxtMsg sendTxtMsg) {
        //对象转换为XML字符串
        XStream xstream = new XStream();
        xstream.alias("xml", sendTxtMsg.getClass());
//            xstream.alias("item", new SendNews().getClass());

//            logger.info("SendTxtMsg:"+xstream.toXML(sendTxtMsg));
        return xstream.toXML(sendTxtMsg);
    }
}